import number from "@/mount/global/number"
import { generateID, isInArray } from "@/utils"
import { random, range } from "lodash"
import { findFollowData, findNextData, identifyGarrison, identifyNext } from "./constant"

/* 战争用原型拓展 */
export default class CreepWarMisson extends Creep {
    public handle_task_double():void{
        this.notifyWhenAttacked(false)
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (this.room.name == this.memory.belong)
        {
            if (this.memory.role == 'double-dismantle')
            {
                if (!this.BoostCheck(['work','move','tough'])) return
            }
            else if (this.memory.role == 'double-attack')
            {
                if (!this.BoostCheck(['attack','move','tough'])) return
            }
            else if (this.memory.role == 'double-heal')
            {
                if (!this.BoostCheck(['heal','move','tough'])) return
            }
        }
        if (!this.memory.double)
        {
            if (this.memory.role == 'double-heal')
            {
                /* 由heal来进行组队 */
                var disrole = misson.teamType == 'dismantle'?'double-dismantle':'double-attack'
                if (Game.time % 7 == 0)
                {
                    var disCreep = this.pos.findClosestByRange(FIND_MY_CREEPS,{filter:(creep)=>{
                        return creep.memory.role == disrole && !creep.memory.double
                    }})
                    if (disCreep)
                    {
                        this.memory.double = disCreep.name
                        disCreep.memory.double = this.name
                        this.memory.captain = true
                        disCreep.memory.captain = false
                    }
                }
            }
            return
        }
        if (this.memory.captain)
        {
            this.memory.standed = true
            if (this.memory.role == 'double-heal')
            {
                /* 寻找自己和自己的爬，谁血量最少奶谁，血量一样奶自己 */
                if (!Game.creeps[this.memory.double])   // 爬虫死了或者在跨shard
                {
                    this.heal(this)
                    return
                }
                else
                {
                    if(this.pos.isNearTo(Game.creeps[this.memory.double]))
                    {
                        if (Game.creeps[this.memory.double].hits > this.hits) this.heal(Game.creeps[this.memory.double])
                        else this.heal(this)
                    }
                    else
                    {
                        this.heal(this)
                        if  ((!isInArray([0,49],this.pos.x) && !isInArray([0,49],this.pos.y)))
                        return
                    }
                    /* 有疲劳就不行走 */
                    if (this.fatigue || Game.creeps[this.memory.double].fatigue) return
                }
            }
            else if (this.memory.role == 'double-attack' || this.memory.role == 'double-dismantle')
            {
                if (Game.flags[`${this.memory.belong}/arrive`])
                {
                    this.goTo(Game.flags[`${this.memory.belong}/arrive`].pos,0)
                    return
                }
                if (!Game.creeps[this.memory.double]) return
                if (this.fatigue || Game.creeps[this.memory.double].fatigue) return
                if (Game.creeps[this.memory.double] && !this.pos.isNearTo(Game.creeps[this.memory.double]) && (!isInArray([0,49],this.pos.x) && !isInArray([0,49],this.pos.y)))
                return
            }
            /* 去目标房间 */
            if (Game.shard.name != shard || this.room.name != roomName)
            {
                this.arriveTo(new RoomPosition(24,24,roomName),24,shard)
            }
            else
            {
                var redFlag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                    return flag.color == COLOR_RED
                }})
                // var redFlagRange = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
                //     return flag.color == COLOR_RED
                // }})
                if (!redFlag)
                {
                    var Attstructure = this.pos.findClosestByPath(FIND_STRUCTURES,{filter:(stru)=>{
                        return !isInArray(Object.keys(Memory.ControlPlan),stru.room.name) && isInArray(['rampart','spawn','terminal','extension','tower','link'],stru.structureType)
                    }})
                    if (Attstructure)
                    {
                        if (this.attack(Attstructure)==ERR_NOT_IN_RANGE)
                        this.goTo(Attstructure.pos,1)
                    }
                    return
                }
                // if (!redFlag && redFlagRange)
                // {
                //     /* 寻找redFlagRange附近的rampart */
                //     var Attstructure = redFlagRange.pos.findClosestByPath(FIND_STRUCTURES,{filter:(stru)=>{
                //         return !isInArray(Object.keys(Memory.ControlPlan),stru.room.name) && isInArray(['rampart'],stru.structureType)
                //     }})
                //     if (Attstructure)
                //     Attstructure.pos.createFlag(generateID(),COLOR_RED)
                //     return
                // }
                // else if (!redFlag && !redFlagRange)
                // {
                //     var Attstructure = this.pos.findClosestByPath(FIND_STRUCTURES,{filter:(stru)=>{
                //         return !isInArray(Object.keys(Memory.ControlPlan),stru.room.name) && isInArray(['rampart'],stru.structureType)
                //     }})
                //     if (Attstructure)
                //     Attstructure.pos.createFlag(generateID(),COLOR_RED)
                //     return
                // }
                if (this.memory.role == 'double-heal')
                {
                    if (this.pos.x <= 48 && this.pos.x >= 1 && this.pos.y <= 48 && this.pos.y >= 1)
                    {
                        this.memory.captain = false
                        Game.creeps[this.memory.double].memory.captain = true
                        
                    }
                    this.goTo(redFlag.pos,1)
                    
                }
                else if (this.memory.role == 'double-attack' || this.memory.role == 'double-dismantle')
                {

                    /* 查找不在rampart上的爬 */
                    if (this.memory.role == 'double-attack')
                    {
                        var enemy = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                            return !isInArray(Memory.whitesheets,creep.owner.username) && !creep.pos.GetStructure('rampart')
                        }})
                        if (enemy && Game.flags[`${this.memory.belong}/kill`])
                        {
                            this.goTo(enemy.pos,1)
                            this.attack(enemy)
                            return
                        }
                    }
                    var yellowFlag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
                        return flag.color == COLOR_YELLOW
                    }})
                    if (yellowFlag){this.goTo(yellowFlag.pos,0);return}
                    if (!this.pos.isNearTo(redFlag))
                    {
                        this.goTo(redFlag.pos,1)
                    }
                    else
                    {
                        var disStructure = redFlag.pos.lookFor(LOOK_STRUCTURES)[0]
                        if(disStructure) this.memory.role == 'double-attack'?this.attack(disStructure):this.dismantle(disStructure)
                        else {redFlag.remove()}
                        
                    }
                }

            }
        }
        else
        {
            this.memory.standed = false
            if (this.memory.role == 'double-heal')
            {
                this.heal(this)
                if (!Game.creeps[this.memory.double]){this.suicide();return}
                else
                {
                    if (this.pos.isNearTo(Game.creeps[this.memory.double]))
                    {
                        var caption_hp = Game.creeps[this.memory.double].hits
                        var this_hp = this.hits
                        if (this_hp == this.hitsMax && caption_hp == Game.creeps[this.memory.double].hitsMax) this.heal(Game.creeps[this.memory.double])
                        if (caption_hp < this_hp)
                        {
                            this.heal(Game.creeps[this.memory.double])
                        }
                        else
                        {
                            
                            this.heal(this)
                        }
                        let otherCreeps = this.pos.findInRange(FIND_MY_CREEPS,3,{filter:(creep)=>{return creep.hits < creep.hitsMax - 300}})
                        if (otherCreeps[0] && this.hits == this.hitsMax && Game.creeps[this.memory.double].hits == Game.creeps[this.memory.double].hitsMax)
                        {
                            if (otherCreeps[0].pos.isNearTo(this))
                            this.heal(otherCreeps[0])
                            else this.rangedHeal(otherCreeps[0])
                        }
                    }
                    else
                    {
                        this.heal(this)
                        if(this.room == roomName)
                        {
                            this.moveTo(Game.creeps[this.memory.double])
                            return
                        }
                    }
                }
            }
            else
            {
                /* 如果在星门附件且找不到目标虫 暂缺 */
                var disCreepName = this.memory.double
                var portal = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(stru)=>{
                    return stru.structureType == 'portal'
                }})
                if (disCreepName == null || (!Game.creeps[disCreepName] && !portal)) return
                if (!Game.creeps[disCreepName] && portal){this.arriveTo(new RoomPosition(25,25,roomName),20,shard);return}
                if (Game.shard.name == shard && !Game.creeps[disCreepName]) return
                var disCreep = Game.creeps[disCreepName]
            }
            this.moveTo(Game.creeps[this.memory.double].pos)
        }

    }

    public handle_task_suqard():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (this.memory.controlledBySquardFrame)
        {
            /* 如果是框架控制，转由框架控制 */
            if (!Memory.squardMemory) Memory.squardMemory = {}
            if (!Memory.squardMemory[this.memory.MissonData.MisssonID])
            {
                Memory.squardMemory[this.memory.MissonData.MisssonID] = {
                    creepData:this.memory.squard,
                    sourceRoom:this.memory.belong,
                    presentRoom:this.room.name,
                    disRoom:misson.disRoom,
                    ready:false,
                    array:'free',
                    sourceShard:this.memory.shard,
                    disShard:this.memory.targetShard,
                    squardType:'XR'
                }
            }
            /* 赋予全局Memory记忆后，即可交由全局四人小队框架控制 */
            return
        }
        if (this.room.name == this.memory.belong && this.memory.shard == Game.shard.name)
        {
            var thisRoom = Game.rooms[this.memory.belong]
            /* boost检查 暂缺 */
            if (!this.BoostCheck(['ranged_attack','move','tough','heal'])) return
   
            /* 组队检查 */
            if (!thisRoom.memory.squardData) Game.rooms[this.memory.belong].memory.squardData = {}
            var MissonSquardData = thisRoom.memory.squardData[this.memory.MissonData.MisssonID]
            if (!MissonSquardData) thisRoom.memory.squardData[this.memory.MissonData.MisssonID] = {}
            if (!this.memory.MissonData.MisssonID) return
            if (!MissonSquardData) return
            if (this.memory.creepType == 'heal')
            {
                
                if (Object.keys(MissonSquardData).length <= 0 ) MissonSquardData[this.name] = {position:'↙',index:1,role:this.memory.role,creepType:this.memory.creepType}
                if (Object.keys(MissonSquardData).length == 2 && !isInArray(Object.keys(MissonSquardData),this.name) ) MissonSquardData[this.name] = {position:'↘',index:3,role:this.memory.role,creepType:this.memory.creepType}
        
            }
            else if (this.memory.creepType == 'attack')
            {
                if (Object.keys(MissonSquardData).length== null) return
                if (Object.keys(MissonSquardData).length == 1 && !isInArray(Object.keys(MissonSquardData),this.name) ) MissonSquardData[this.name] = {position:'↖',index:0,role:this.memory.role,creepType:this.memory.creepType}
                if (Object.keys(MissonSquardData).length == 3 && !isInArray(Object.keys(MissonSquardData),this.name) ) MissonSquardData[this.name] = {position:'↗',index:2,role:this.memory.role,creepType:this.memory.creepType}
            
            }
            if (Object.keys(thisRoom.memory.squardData[this.memory.MissonData.MisssonID]).length == 4 && !this.memory.squard)
            {
                console.log(this.name, '添加squard记忆')
                this.memory.squard = thisRoom.memory.squardData[this.memory.MissonData.MisssonID]
                return
            }
            /* 朝前面的爬移动 */
            if (!this.memory.squard) return
            /* 检查是否所有爬虫都赋予记忆了 */
            for (var mem in this.memory.squard)
            {
                if (!Game.creeps[mem]) return
                if (!Game.creeps[mem].memory.squard)return
            }
        }
        /* 防止路上被截胡 */
        if (this.getActiveBodyparts('ranged_attack'))
        {
            var enemy = this.pos.findInRange(FIND_HOSTILE_CREEPS,3)
            if (enemy[0])
            this.rangedAttack(enemy[0])
        }
        if (this.getActiveBodyparts('heal'))
        {
            this.heal(this)
        }
        for (var cc in this.memory.squard)
        {
        if (Game.creeps[cc] && Game.creeps[cc].fatigue) return
        }
        if (this.memory.squard[this.name].index != 3 && (!isInArray([0,49],this.pos.x) && !isInArray([0,49],this.pos.y)))
        {
            var followCreepName = findNextData(this)
            if (followCreepName == null) return
            var portal = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(stru)=>{
                return stru.structureType == 'portal'
             }})
             var followCreep = Game.creeps[followCreepName]
            if (!followCreep && portal) {return}
            if (followCreep)
            {
            // 跟随爬不靠在一起就等一等
            if (!this.pos.isNearTo(followCreep)) return
            }
            
        }
        if (this.memory.squard[this.name].index != 0)
        {
            var disCreepName = findFollowData(this)
            var portal = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(stru)=>{
                return stru.structureType == 'portal'
            }})
            if (disCreepName == null || (!Game.creeps[disCreepName] && !portal)) return
            if (!Game.creeps[disCreepName] && portal){this.arriveTo(new RoomPosition(25,25,roomName),20,shard);return}
            if (Game.shard.name == shard && !Game.creeps[disCreepName]) return
            var disCreep = Game.creeps[disCreepName]
            if (this.room.name == this.memory.belong)  this.goTo(disCreep.pos,0)
            else this.moveTo(disCreep,{ignoreCreeps:true})
        }
        else
        {

        }
        /* 判断在不在目标房间入口房间 */
        if (identifyNext(this.room.name,roomName) == false)
        {
            if (this.memory.squard[this.name].index == 0)
            this.arriveTo(new RoomPosition(25,25,roomName),20,shard)
        }
        else
        {
            if (this.memory.squard[this.name].index == 0)
            {
                this.say('🔪',true)
                if (!this.memory.arrived)
                {
                    var blueFlag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
                        return flag.color == COLOR_BLUE
                    }})
                    if (blueFlag)
                    this.arriveTo(blueFlag?blueFlag.pos:new RoomPosition(25,25,this.room.name),blueFlag?5:10,shard)
                    /* 寻找周围有没有空地 */
                    if (identifyGarrison(this) && shard == Game.shard.name)
                    {
                        this.memory.arrived = true
                        return
                    }
                }
                else
                {
                    // 到达了的逻辑
                    for (var crp in this.memory.squard)
                    {
                        if (Game.creeps[crp])
                            Game.creeps[crp].memory.controlledBySquardFrame = true
                    }
                }
            }
        }
    }

    public handle_task_squard():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        var squardID = misson.squardID
        if (this.memory.controlledBySquardFrame)
        {
            /* 说明到达指定房间，并到达合适位置了 */
            /* 添加战争框架控制信息 */
            if (!Memory.squardMemory) Memory.squardMemory = {}
            if (!squardID) {this.say("找不到squardID!");return}
            if (!Memory.squardMemory[squardID])
            {
                Memory.squardMemory[squardID] = {
                    creepData:this.memory.squard,
                    sourceRoom:this.memory.belong,
                    presentRoom:this.room.name,
                    disRoom:misson.disRoom,
                    ready:false,
                    array:'free',
                    sourceShard:this.memory.shard,
                    disShard:this.memory.targetShard,
                    squardType:misson.sType
                }
            }
            /* 赋予全局Memory记忆后，即可交由全局四人小队框架控制 */
            return
        }
        else
        {
            /* 任务开始前准备 */
            if (this.room.name == this.memory.belong && this.memory.shard == Game.shard.name)
            {
                var thisRoom = Game.rooms[this.memory.belong]
                /* boost检查 */
                if(this.getActiveBodyparts('move')>0)
                {
                    if (!this.BoostCheck([,'move'])) return
                }
                if(this.getActiveBodyparts('heal')>0)
                {
                    if (!this.BoostCheck([,'heal'])) return
                }
                if(this.getActiveBodyparts('work')>0)
                {
                    if (!this.BoostCheck([,'work'])) return
                }
                if(this.getActiveBodyparts('attack')>0)
                {
                    if (!this.BoostCheck([,'attack'])) return
                }
                if(this.getActiveBodyparts('ranged_attack')>0)
                {
                    if (!this.BoostCheck([,'ranged_attack'])) return
                }
                if(this.getActiveBodyparts('tough')>0)
                {
                    if (!this.BoostCheck([,'tough'])) return
                }
                /* 组队检查 */
                if(!squardID) return
                if (!this.memory.MissonData.MisssonID) return
                if (!thisRoom.memory.squardData) Game.rooms[this.memory.belong].memory.squardData = {}
                var MissonSquardData = thisRoom.memory.squardData[squardID]
                if (!MissonSquardData) thisRoom.memory.squardData[squardID] = {}
                if (!MissonSquardData) return
                if (this.memory.creepType == 'heal')
                {
                    if (Object.keys(MissonSquardData).length <= 0 ) MissonSquardData[this.name] = {position:'↙',index:1,role:this.memory.role,creepType:this.memory.creepType}
                    if (Object.keys(MissonSquardData).length == 2 && !isInArray(Object.keys(MissonSquardData),this.name) ) MissonSquardData[this.name] = {position:'↘',index:3,role:this.memory.role,creepType:this.memory.creepType}
            
                }
                else if (this.memory.creepType == 'attack')
                {
                    if (Object.keys(MissonSquardData).length == 1 && !isInArray(Object.keys(MissonSquardData),this.name) ) MissonSquardData[this.name] = {position:'↖',index:0,role:this.memory.role,creepType:this.memory.creepType}
                    if (Object.keys(MissonSquardData).length == 3 && !isInArray(Object.keys(MissonSquardData),this.name) ) MissonSquardData[this.name] = {position:'↗',index:2,role:this.memory.role,creepType:this.memory.creepType}
                }
                if (Object.keys(thisRoom.memory.squardData[this.memory.MissonData.MisssonID]).length == 4 && !this.memory.squard)
                {
                    console.log(this.name, '添加squard记忆')
                    this.memory.squard = thisRoom.memory.squardData[this.memory.MissonData.MisssonID]
                    return
                }
                /* 朝前面的爬移动 */
                if (!this.memory.squard) return
                 /* 检查是否所有爬虫都赋予记忆了 */
                for (var mem in this.memory.squard)
                {
                    if (!Game.creeps[mem]) return
                    if (!Game.creeps[mem].memory.squard)return
                }
            }
            /* 到达任务房间前自卫 */
            if (this.getActiveBodyparts('ranged_attack'))
            {
                var enemy = this.pos.findInRange(FIND_HOSTILE_CREEPS,3,{filter:(creep)=>{
                    return !isInArray(Memory.whitesheets,creep.owner.username)
                }})
                if (enemy[0])
                this.rangedAttack(enemy[0])
            }
            if (this.getActiveBodyparts('heal'))
            {
                var bol = true
                for (var i in this.memory.squard)
                {
                    if(Game.creeps[i] && Game.creeps[i].hits < Game.creeps[i].hitsMax && this.pos.isNearTo(Game.creeps[i]))
                    {
                        bol =false
                        this.heal(Game.creeps[i])
                    }
                }
                if(bol) this.heal(this)
            }
            /* 线性队列行走规则设定 */
            for (var cc in this.memory.squard)
            {
                if (Game.creeps[cc] && Game.creeps[cc].fatigue) return
            }
            if (this.memory.squard[this.name].index != 3 && (!isInArray([0,49],this.pos.x) && !isInArray([0,49],this.pos.y)))
            {
                var followCreepName = findNextData(this)
                if (followCreepName == null) return
                var portal = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(stru)=>{
                    return stru.structureType == 'portal'
                 }})
                 var followCreep = Game.creeps[followCreepName]
                if (!followCreep && portal) {return}
                if (followCreep)
                {
                // 跟随爬不靠在一起就等一等
                if (!this.pos.isNearTo(followCreep)) return
                }
                
            }
            if (this.memory.squard[this.name].index != 0)
            {
                var disCreepName = findFollowData(this)
                var portal = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(stru)=>{
                    return stru.structureType == 'portal'
                }})
                if (disCreepName == null || (!Game.creeps[disCreepName] && !portal)) return
                if (!Game.creeps[disCreepName] && portal){this.arriveTo(new RoomPosition(25,25,roomName),20,shard);return}
                if (Game.shard.name == shard && !Game.creeps[disCreepName]) return
                var disCreep = Game.creeps[disCreepName]
                if (this.room.name == this.memory.belong)  this.goTo(disCreep.pos,0)
                else this.moveTo(disCreep,{ignoreCreeps:true})
            }
            /* 判断在不在目标房间入口房间 */
            if (identifyNext(this.room.name,roomName) == false)
            {
                if (this.memory.squard[this.name].index == 0)
                this.arriveTo(new RoomPosition(25,25,roomName),20,shard)
            }
            else
            {
                if (this.memory.squard[this.name].index == 0)
                {
                    this.say('🔪',true)
                    if (!this.memory.arrived)
                    {
                        var blueFlag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
                            return flag.color == COLOR_BLUE
                        }})
                        if (blueFlag)
                        this.arriveTo(blueFlag.pos,5,shard)
                        else
                        this.arriveTo(new RoomPosition(25,25,this.room.name),10,shard)
                        /* 寻找周围有没有空地 */
                        if (identifyGarrison(this) && shard == Game.shard.name)
                        {
                            this.memory.arrived = true
                            return
                        }
                    }
                    else
                    {
                        // 到达了的逻辑
                        for (var crp in this.memory.squard)
                        {
                            if (Game.creeps[crp])
                                Game.creeps[crp].memory.controlledBySquardFrame = true
                        }
                    }
                }
            }
        }
    }

    public handle_task_defend():void{
        var ControlData = Memory.ControlPlan[this.memory.belong]
        if (!ControlData) return
        var DefendData = ControlData.defendPlan
        if (!DefendData) return
        if (this.memory.role == 'defend-attack')
        {
            if (!global.TowerData[this.memory.belong].towerOccupy) global.TowerData[this.memory.belong].towerOccupy = this.name
            if (global.TowerData[this.memory.belong].towerOccupy == this.name)
            {
                var towerlist = global.TowerData[this.memory.belong].towerObjectList
                if (towerlist && this.hits < this.hitsMax)
                {
                    for (var i of towerlist)
                        i.heal(this)
                }
            }
            if(Game.flags[`${this.memory.belong}/defend`]){this.goTo(Game.flags[`${this.memory.belong}/defend`].pos,0);return}
            if (!this.BoostCheck(['move','attack'])) return
            /* 开始追敌人，打 */
            var nearCreep = this.pos.findInRange(FIND_HOSTILE_CREEPS,1,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.name)
            }})
            if (nearCreep.length > 0)
            {
                this.attack(nearCreep[0])
                if(this.hits/this.hitsMax > 0.8)
                {
                    var towerlist = global.TowerData[this.memory.belong].towerObjectList
                    if (towerlist)
                    {
                        for (var i of towerlist)
                            i.attack(nearCreep[0])
                    }
                }
            }
            /* 寻路 */
            var hostileCreep = Game.rooms[this.memory.belong].find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.name)
            }})
            var center = new RoomPosition(DefendData.x,DefendData.y,this.memory.belong)
            var ramparts = Game.rooms[this.memory.belong].find(FIND_MY_STRUCTURES,{filter:(stru)=>{
                return stru.structureType == 'rampart' && stru.pos.inRangeTo(center,DefendData.range)
            }})
            if (hostileCreep.length > 0)
            {
                if (ramparts.length > 0)
                {
                    var ramData= {num:0,ram:null}
                    //if (Game.time % 5 == 0)
                    for(var r of ramparts)
                    {
                        var nearCreeps = r.pos.findInRange(FIND_HOSTILE_CREEPS,1,{filter:(creep)=>{
                            return !isInArray(Memory.whitesheets,creep.name)
                        }})
                        if (nearCreeps.length > ramData.num) {ramData.num = nearCreeps.length;ramData.ram = r}
                    }
                    if (!ramData.ram)
                    {
                        //this.say("no-ram")
                        var closestCreep = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                            return !isInArray(Memory.whitesheets,creep.name)
                        }})
                        if (closestCreep.pos.inRangeTo(center,DefendData.range+3) && !this.pos.inRangeTo(closestCreep.pos,3))
                        {
                            /* 找离虫子最近的rampart */
                            var nearstram = closestCreep.pos.findClosestByRange(FIND_MY_STRUCTURES,{filter:(stru)=>{
                                return stru.structureType == 'rampart' && stru.pos.inRangeTo(center,DefendData.range) && stru.pos.GetStructureList(['extension','link','observer','tower','controller','extractor']).length <= 0
                            }})
                            this.goTo_defend(nearstram.pos,0,DefendData)
                        }
                    }
                    else
                    {
                        //this.say("ram")
                        var thisPos = ramData.ram.pos as RoomPosition
                        if (thisPos.GetStructureList(['extension','link','observer','tower','controller','extractor']).length > 0)
                        {
                            //this.goTo(thisPos,1)
                            this.goTo_defend(ramData.ram.pos,1,DefendData)
                        }
                        else
                        this.goTo(thisPos,0)
                        this.goTo_defend(ramData.ram.pos,0,DefendData)
                    }
                }
                else
                {
                    this.moveTo(hostileCreep[0])
                }
                // console.log(DefendData.x+DefendData.range)
                if (this.pos.x > DefendData.x+DefendData.range || this.pos.x < DefendData.x - DefendData.range || this.pos.y > DefendData.y+DefendData.range || this.pos.y < DefendData.y - DefendData.range)
                {
                    this.moveTo(new RoomPosition(DefendData.x,DefendData.y,this.memory.belong))
                }
            }
            else
            {
                /* 没发现就删除任务 */
                Game.rooms[this.memory.belong].DeleteMisson(this.memory.MissonData.MisssonID)
                this.memory.MissonData = {}
                return
            }
        }
    }

    public handle_task_dismantle():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (this.room.name == this.memory.belong && this.memory.shard == Game.shard.name)
        {
            if (!this.BoostCheck(['work','move'])) return
        }
        if (Game.shard.name != shard || this.room.name != roomName)
        {
            this.arriveTo(new RoomPosition(24,24,roomName),23,shard)
        }
        else
        {
            var flag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_ORANGE
            }})
            if (!flag)
            {
                var clostStructure = this.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES,{filter:(struc)=>{
                    return !isInArray([STRUCTURE_CONTROLLER,STRUCTURE_WALL],struc.structureType)
                }})
                if (clostStructure)
                {
                    if (!this.pos.isNearTo(clostStructure)) this.goTo(clostStructure.pos,1)
                    else this.dismantle(clostStructure)
                    return
                }
                else
                    return
            }
            var structures = flag.pos.lookFor(LOOK_STRUCTURES)
            if (structures.length > 0)
            {
            if (!this.pos.isNearTo(structures[0]))
                this.goTo(structures[0].pos,1)
            else this.dismantle(structures[0])
            }
            else
            {
                flag.remove()
            }
        }
    }

    public handle_task_claimattack():void{
        this.notifyWhenAttacked(false)
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (Game.shard.name != shard || this.room.name != roomName)
        {
            this.arriveTo(new RoomPosition(24,24,roomName),23,shard)
        }
        else
        {
            if (!this.room.controller) return
            if (!this.pos.isNearTo(this.room.controller))
            {
                this.goTo(this.room.controller.pos,1)
            }
            if (this.room.controller.owner && this.room.controller.owner.username != this.owner.username)
            this.attackController(this.room.controller)
            else this.reserveController(this.room.controller)
            if (this.room.controller.upgradeBlocked)
            {
                let cons = this.pos.findClosestByPath(FIND_HOSTILE_CONSTRUCTION_SITES,{filter:(cons)=>{
                    return cons.progress/cons.progressTotal >= 0.2
                }})
                if (cons){this.goTo(cons.pos,0)}
            }
        }
    }

    public handle_om_kill():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        this.notifyWhenAttacked(false)
        if (Game.shard.name != shard || this.room.name != roomName)
        {
            this.arriveTo(new RoomPosition(24,24,roomName),20,shard)
        }
        else
        {
            /* 寻找虫卵建筑 */
            let stru = this.pos.findClosestByRange(FIND_HOSTILE_STRUCTURES,{filter:(stru)=>{
                return (stru.structureType == 'spawn'  || stru.structureType=='tower')&& !stru.pos.GetStructure('rampart')
            }})
            if (stru && this.getActiveBodyparts('work')){
                if (this.dismantle(stru) == ERR_NOT_IN_RANGE)
                    this.goTo(stru.pos,1)
                return
            }
            /* 寻找建筑超过10%的工地 */
            let cons = this.pos.findClosestByRange(FIND_HOSTILE_CONSTRUCTION_SITES,{filter:(cons)=>{
                return cons.progress/cons.progressTotal>0.1
            }})
            if (cons){
                this.goTo(cons.pos,0)
                return
            }
            let spawn = this.pos.findClosestByRange(FIND_HOSTILE_CONSTRUCTION_SITES,{filter:(cos)=>{
                return cos.structureType =='spawn' && cos.pos.lookFor(LOOK_CREEPS).length <= 0
            }})
            if (spawn){
                this.goTo(spawn.pos,0)
                return
            }
        }
    }
}